/*      $Id: ca_terraquati_canopy_http_impl_HTTPSocketImpl.c 1069 2010-04-26 02:36:16Z phrakt $    */
/*
 * Copyright (c) 2007 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <canopy/log.h>

#include <http/http.h>
#include <http/sock.h>

#include "mod_java.h"



#define NATIVE_SOCKET_CLASS	"ca.terraquati.canopy.http.impl.HTTPSocketImpl"




static void HTTPSocketImpl_close    (JNIEnv *, jclass);
static jint HTTPSocketImpl_getState (JNIEnv *, jclass);
static jint HTTPSocketImpl_recv     (JNIEnv *, jclass, jbyteArray, jint, jlong);
static jint HTTPSocketImpl_send     (JNIEnv *, jclass, jbyteArray, jint, jlong);


static const JNINativeMethod HTTPSocketImpl_methods[] = {
	{ "close",        "()V",       HTTPSocketImpl_close      },
	{ "getState",     "()I",       HTTPSocketImpl_getState   },
	{ "recv",         "([BIJ)I",   HTTPSocketImpl_recv       },
	{ "send",         "([BIJ)I",   HTTPSocketImpl_send       },
};

/*
 *
 *
 *
 */
int
mod_java_load_HTTPSocketImpl(JNIEnv *env)
{
	jint ret;
	jclass class;

	if ((class = mod_java_getclass(env, NATIVE_SOCKET_CLASS, 0)) == NULL)
		return (-1);

	ret = (*env)->RegisterNatives(env, class, HTTPSocketImpl_methods, 4);
	if (ret != JNI_OK) {
		canopy_log(CNP_LOG_ERR, "failed to register native methods");
		return (-1);
	}

	(*env)->DeleteLocalRef(env, class);

	return (0);
}

/*
 *
 *
 */
static void
HTTPSocketImpl_close(JNIEnv *env, jobject obj)
{
	http_sock_t *sock = mod_java_getpeer(env, obj, "peer_hdl");

	if (sock == NULL) {
		/* throw an exception */
		return;
	}

	http_sock_close(sock);
}

/*
 *
 *
 */
static jint
HTTPSocketImpl_getState(JNIEnv *env, jobject obj)
{
	jint state;
	http_sock_t *sock = mod_java_getpeer(env, obj, "peer_hdl");

	state = http_sock_getstate(sock);

	return (state);
}

/*
 *
 *
 */
static jint
HTTPSocketImpl_recv(JNIEnv *env, jobject obj, jbyteArray data, jint length,
    jlong timeout)
{
	ssize_t ret;
	jboolean copy = JNI_FALSE;
	jbyte *bytes;
	http_sock_t *sock = mod_java_getpeer(env, obj, "peer_hdl");

	if (sock == NULL) {
	}

	bytes = (*env)->GetByteArrayElements(env, data, &copy);

	ret = http_sock_recv(sock, bytes, (size_t)length);

	(*env)->ReleaseByteArrayElements(env, data, bytes, 0);

	return (jint)ret;
}

/*
 *
 *
 */
static jint
HTTPSocketImpl_send(JNIEnv *env, jobject obj, jbyteArray data, jint length,
    jlong timeout)
{
	ssize_t ret;
	jboolean copy = JNI_FALSE;
	jbyte *bytes;
	http_sock_t *sock = mod_java_getpeer(env, obj, "peer_hdl");

	if (sock == NULL) {
	}

	bytes = (*env)->GetByteArrayElements(env, data, &copy);

	ret = http_sock_send(sock, bytes, (size_t)length);

	(*env)->ReleaseByteArrayElements(env, data, bytes, 0);

	return (jint)ret;
}
